/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * cadProdutoJInternalFrame.java
 *
 * Created on 27/10/2009, 21:03:56
 */
package br.com.fatecpg.zanotti.jframes.consultas;

import br.com.fatecpg.zanotti.acessobanco.*;
import br.com.fatecpg.zanotti.jframes.cadastros.cadUsuarioJInternalFrame;
import br.com.fatecpg.zanotti.modelo.Funcionario;
import br.com.fatecpg.zanotti.modelo.Usuario;

/**
 *
 * @author Luiz Zanotti
 */
public class conUsuarioJInternalFrame extends javax.swing.JInternalFrame {

    /** Creates new form cadProdutoJInternalFrame */
    public conUsuarioJInternalFrame() {

        initComponents();
        conFuncjTable.getColumnModel().getColumn(0).setMaxWidth(90);
        conFuncjTable.getColumnModel().getColumn(0).setMinWidth(90);
        conFuncjTable.getColumnModel().getColumn(1).setMaxWidth(300);
        conFuncjTable.getColumnModel().getColumn(1).setMinWidth(210);




    }

    public void atualizaTable() {
        conFuncjTable.removeAll();
        conTableUsuario con = new conTableUsuario();
        conFuncjTable = con.ConsultaTabela(conFuncjTable);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        conFuncjTable = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        alterarjButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        novojButton = new javax.swing.JButton();
        atualizajButton = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);
        setTitle("Consulta de Funcionários");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        conFuncjTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Código", "Nome do Funcionário"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(conFuncjTable);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 390, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        alterarjButton.setText("Alterar");
        alterarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alterarjButtonActionPerformed(evt);
            }
        });

        jButton1.setText("Excluir");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        novojButton.setText("Novo");
        novojButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                novojButtonActionPerformed(evt);
            }
        });

        atualizajButton.setText("Atualiza");
        atualizajButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                atualizajButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
                    .addComponent(novojButton, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
                    .addComponent(alterarjButton, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
                    .addComponent(atualizajButton, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(alterarjButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(novojButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(atualizajButton)
                .addContainerGap(67, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void atualizajButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_atualizajButtonActionPerformed
        atualizaTable();
    }//GEN-LAST:event_atualizajButtonActionPerformed

    private void novojButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_novojButtonActionPerformed
cadUsuarioJInternalFrame.getInstancia().resetarValores();
        cadUsuarioJInternalFrame.getInstancia().setVisible(true);
        this.setVisible(false);

    }//GEN-LAST:event_novojButtonActionPerformed
    Funcionario func;

    private void alterarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alterarjButtonActionPerformed
      if(conFuncjTable.getSelectedRow()<0){
javax.swing.JOptionPane.showMessageDialog(null, "Nenhuma linha selecionada!", "Atenção", 2);}
      else{
        this.setVisible(false);
        String str = conFuncjTable.getValueAt(conFuncjTable.getSelectedRow(), 0).toString();

        func = new conTableUsuario().ConsultaFuncionarioPorLinha(str);

        cadUsuarioJInternalFrame.getInstancia().setVisible(true);

        //TERMINAR ESSA ZONA AQUI - Até de Noite!! 17/11



        cadUsuarioJInternalFrame.getInstancia().setCargojComboBox(Integer.parseInt(func.getCDCARGO()));



        cadUsuarioJInternalFrame.getInstancia().setCdfuncjTextField(func.getCDFUNCIONARIO());
        cadUsuarioJInternalFrame.getInstancia().setCnhjTextField(func.getCNHFUNCIONARIO());
        cadUsuarioJInternalFrame.getInstancia().setCpfjFormattedTextField(func.getCPFFUNCIONARIO());
        cadUsuarioJInternalFrame.getInstancia().setDataadmjFormattedTextField1(func.getDTADMISSAO());
        cadUsuarioJInternalFrame.getInstancia().setDatajFormattedTextField(func.getDTNASCIMENTO());
        cadUsuarioJInternalFrame.getInstancia().setLojajComboBox(Integer.parseInt(func.getCDLOJA()));
        cadUsuarioJInternalFrame.getInstancia().setNomejTextField(func.getNMFUNCIONARIO());
        cadUsuarioJInternalFrame.getInstancia().setRgjTextField(func.getRGFUNCIONARIO());
        cadUsuarioJInternalFrame.getInstancia().setTelefonejFormattedTextField(func.getCDTELEFONE());


        boolean boolcon = new conTableUsuario().ConsultaUsuarioPorLinha(str);



        cadUsuarioJInternalFrame.getInstancia().setCriarUsrjCheckBox(boolcon);

        if (boolcon) {
            cadUsuarioJInternalFrame.getInstancia().setjPasswordField1(Usuario.getInstancia().getCDSENHA());
            cadUsuarioJInternalFrame.getInstancia().setjPasswordField2(Usuario.getInstancia().getCDSENHA());
        }

        cadUsuarioJInternalFrame.getInstancia().setTitle("Alteração de Cadastro de Funcionário");
        cadUsuarioJInternalFrame.getInstancia().setCadastrarjButton("Salvar");


      }
    }//GEN-LAST:event_alterarjButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
      if(conFuncjTable.getSelectedRow()<0){
javax.swing.JOptionPane.showMessageDialog(null, "Nenhuma linha selecionada!", "Atenção", 2);}
      else{
        String str = conFuncjTable.getValueAt(conFuncjTable.getSelectedRow(), 0).toString();
        dmlUsuario.deletar(str);
        dmlFuncionario.deletar(str);
        atualizaTable();}
    }//GEN-LAST:event_jButton1ActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        atualizaTable();
    }//GEN-LAST:event_formInternalFrameOpened
    private static conUsuarioJInternalFrame p;

    public static conUsuarioJInternalFrame getInstancia() {
        if (p == null) {
            p = new conUsuarioJInternalFrame();
        }
        return p;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton alterarjButton;
    private javax.swing.JButton atualizajButton;
    private javax.swing.JTable conFuncjTable;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton novojButton;
    // End of variables declaration//GEN-END:variables
}
